package cqut.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cqut.demo.dao.entity.User;
import cqut.demo.dao.mapper.UserMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

/**
 * User控制器
 * @author wuyoushan
 * @date 2016/12/25.
 */
@Controller
@RequestMapping(value = "/User")
public class UserController {
    //日记记录
    private static final Log logger= LogFactory.getLog(UserController.class);

    @Autowired
    private UserMapper userMapper;

    //跳转到添加用户页面
    @RequestMapping(value="/add.do",method = RequestMethod.GET)
    public String add(){
        return "add";
    }

    //添加用户信息
    @Transactional
    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public String add(@Valid User user, BindingResult result,ModelMap model){
        logger.info("添加用户的信息:"+user.toString());
        model.clear();
        if(result.hasErrors()){
            List<FieldError> listError=result.getFieldErrors();
           for (FieldError f:listError){
               //添加错误信息到
               model.addAttribute(f.getField(),f.getDefaultMessage());
           }
            return "add";
        }
        boolean flag=userMapper.addUser(user);
        if(flag){
            return "redirect:list.do";
        }
        return "add";
    }

    //获取编辑用户的信息
    @RequestMapping(value="/edit.do",method=RequestMethod.GET)
    public String edit(@RequestParam Integer id,ModelMap model){
        User user=userMapper.findUserByID(id);
        model.addAttribute("user",user);
        logger.info("获取编辑用户信息id="+id);
        return "edit";
    }

    //保存编辑用户的信息
    @RequestMapping(value="/edit.do",method =RequestMethod.POST)
    public String edit(User user){
        logger.info("保存编辑用户信息:"+user.toString());
        boolean flag=userMapper.updateUser(user);
        if(flag){
            return "redirect:list.do";
        }
        return "edit";
    }

    //删除用户信息
    @RequestMapping(value="/del.do",method=RequestMethod.GET)
    public String del(@RequestParam Integer id){
        boolean flag=userMapper.delUser(id);
        logger.info("删除用户信息id=:"+id);
        if(flag){
            return "redirect:list.do";
        }
        return "del";
    }

    //获取用户列表
    @RequestMapping(value="/list.do",method=RequestMethod.GET)
    public String list(Model model){
        PageHelper.startPage(1,3);
        List<User> user=userMapper.findAllUser();
        PageInfo<User> pageInfo=new PageInfo<>(user);
        model.addAttribute("list", user);
        return "list";
    }
}
